package com.youxin.busfoundation.utils.security;

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.youxin.foundation.utils.Base64Utils;




public class SecurityAES {


	//@SuppressLint("TrulyRandom")
	public static String encrypt(String input, String key) {
		try {
			byte[] crypted = null;
			SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			//Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, skey);			
			crypted = cipher.doFinal(input.getBytes());
			
			return Base64.getEncoder().encodeToString(crypted);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return null;
	}

	public static String decrypt(String input, String key) {
		try {
			byte[] output = null;
			SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skey);			
			byte[] resultBytes=Base64.getDecoder().decode(input.getBytes());
			output = cipher.doFinal(resultBytes);
			return new String(output);

		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return null;
	}

}
